;; eshell configuration

(add-hook 'eshell-mode-hook (lambda()
			      (outline-minor-mode 1)
			      (setq outline-regexp "^[^#$\n]* [#>]+ "
				    scroll-margin 0
				    eshell-scroll-to-bottom-on-output t
				    eshell-scroll-show-maximum-output t)
			      (add-to-list 'eshell-output-filter-functions
					   'eshell-postoutput-scroll-to-bottom)
			      (define-key eshell-mode-map "<up>" 'eshell-previous-matching-input-from-input)
			      (define-key eshell-mode-map "<down>" 'eshell-next-matching-input-from-input)
			      ))

(setq
 eshell-save-history-on-exit   t
 eshell-history-size           512
 eshell-hist-ignoredups        t
 eshell-cmpl-ignore-case       t
 eshell-cp-interactive-query   t
 eshell-ln-interactive-query   t
 eshell-mv-interactive-query   t
 eshell-rm-interactive-query   t
 eshell-mv-overwrite-files     nil
 ;; aliases-file 里面不能有多余的空行，否则会报正则表达式错误
 ;; eshell-aliases-file       (expand-file-name "_eshell/eshell-alias" init-dir)

 eshell-highlight-prompt   t
 ;; 提示符设置，下面两项必须对应起来，
 ;; 否则会报 read-only，并且不能补全什么的
 eshell-prompt-regexp      "^[^#$\n]* [#>]+ "
 eshell-prompt-function    (lambda nil
			     (concat
			      (abbreviate-file-name
			       (eshell/pwd))
			      (if
				  (=
				   (user-uid)
				   0)
				  " # " " >>> ")))
 )

(add-hook 'eshell-load-hook
	  (lambda()(setq last-command-start-time (time-to-seconds))))
(add-hook 'eshell-pre-command-hook
	  (lambda()(setq last-command-start-time (time-to-seconds))))
(add-hook 'eshell-before-prompt-hook
	  (lambda()
	    (message "spend %g seconds"
		     (- (time-to-seconds) last-command-start-time))))

(defvar ac-source-eshell-pcomplete
  '((candidates . (pcomplete-completions))))
(defun ac-complete-eshell-pcomplete ()
  (interactive)
  (auto-complete '(ac-source-eshell-pcomplete)))
(add-to-list 'ac-modes 'eshell-mode)

(provide 'jerry-eshell)
